from time import sleep
from multiprocessing import Process
from sqlite3 import connect
from Queue import Full
import share
import config

FETCH_SUBSCRIBE = 'SELECT * FROM %s' %(config.STOCK_TBL)

CYCLE = 5
MAX_WAITING_TIME = 10

TAG = 'Producer:'

class Producer(Process):
	def __init__(self):
		Process.__init__(self)
		self.__conn = connect(config.DATABASE)
		self.__cur = self.__conn.cursor()
	def __del__(self):
		self.__cur.close()
	def run(self):
		print TAG, 'process is running...'
		while(True):
			dataCur = self.__cur.execute(FETCH_SUBSCRIBE)
			scrbs = dataCur.fetchall()
			print scrbs
			try:
				for scrb in scrbs:
					print TAG, 'adding scrb data', scrb, 'into GSR login fetion_id'
					#scrb = list(scrb)
					share.GSR.put(scrb, timeout = MAX_WAITING_TIME)

			except Full, e:
				self.__check_consumers()

			sleep(CYCLE)
	
	def __check_consumers(self):
		print TAG, 'checking consumers...' #why not consumed

if __name__ == '__main__':
	producer = Producer()
	producer.start()
	producer.join()
